<template>
    <div>
        <div class="filter">
            <el-form :inline="true" :model="formInline" ref="ruleForm">
                <el-form-item label="管理员名称" prop="nickname">
                    <el-input v-model="formInline.nickname" placeholder="请输入管理员名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="table">
            <div class="add">
                <el-button type="primary" @click="add">新增管理员</el-button>
            </div>
            <div class="data">
                <el-table :data="tableData" border style="width: 100%">
                    <el-table-column prop="nickname" label="管理员名称">
                    </el-table-column>
                    <el-table-column prop="createTime" label="创建时间">
                        <template slot-scope="scope">
                            <div>
                                {{getTime(scope.row.createTime,"yyyy-mm-dd hh:mm:ss")}}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="updateTime" label="更新时间">
                        <template slot-scope="scope">
                            <div>
                                {{getTime(scope.row.updateTime,"yyyy-mm-dd hh:mm:ss")}}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="deleted" label="禁启用">
                        <template slot-scope="scope">
                            <div>
                                <el-switch v-model="scope.row.deleted" :active-value="1" :inactive-value="0" active-color="#13ce66" inactive-color="#ff4949" @change="changeDeleted(scope.row.id)">
                                </el-switch>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="100">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="getAdminById(scope.row.id)">编辑</el-button>
                            <!-- <el-button @click="deleteUser(scope.row.id)" type="text" size="small">删除</el-button> -->
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageInfo.page" :page-sizes="[10, 20]" :page-size="pageInfo.size" layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total">
            </el-pagination>
        </div>
        <el-dialog :title="isAdd?'新增管理员信息':'编辑管理员信息'" width="600px" :visible.sync="dialogFormVisible" center>
            <el-form :model="addForm" label-width="100px" :rules="addRules" ref="addForm">
                <el-form-item label="昵称" prop="nickname">
                    <el-input v-model="addForm.nickname"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type='password' v-model="addForm.password"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="commitForm('addForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import api from "../../../api";
export default {
    name: "admin",
    data() {
        return {
            formInline: {
                nickname: "",
            },
            pageInfo: {
                currentPage: 1,
                pageSize: 10,
                total: 0,
            },
            dialogFormVisible: false,
            addForm: {
                nickname: "",
                password: "",
            },
            tableData: [],
            isAdd: true,
            addRules: {
                nickname: [
                    { required: true, message: "请输入昵称", trigger: "blur" },
                ],
                password: [
                    { required: true, message: "请输入密码", trigger: "blur" },
                ],
            },
        };
    },
    mounted() {
        this.getAdminList();
    },
    methods: {
        onSubmit() {
            this.pageInfo.currentPage = 1;
            this.getAdminList();
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        handleAvatarSuccess(res, file) {
            this.addForm.avatar = URL.createObjectURL(file.raw);
        },
        // 分页获取业主数据
        async getAdminList() {
            try {
                const params = Object.assign(
                    {},
                    this.pageInfo,
                    this.formInline
                );
                const response = await api.admin.getAdminPage(params);
                if (response.data.code === 200) {
                    this.tableData = response.data.data.records;
                    this.pageInfo.total = response.data.data.total;
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 根据ID获取业主
        async getAdminById(id) {
            this.isAdd = false;
            this.dialogFormVisible = true;
            try {
                const response = await api.admin.getAdminById({
                    adminId: id,
                });
                if (response.data.code === 200) {
                    this.addForm = response.data.data;
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 添加或者编辑提交
        async commitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.isAdd) {
                        this.commitAddForm();
                    } else {
                        this.commitEditForm();
                    }
                    this.dialogFormVisible = false;
                    this.addForm = this.$options.data().addForm; //重置表单数据
                }
            });
        },
        // 提交添加用户
        async commitAddForm() {
            try {
                const response = await api.admin.addAdmin(this.addForm);
                if (response.data.code === 200) {
                    this.$message.success("添加成功");
                    this.getAdminList();
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 提交编辑用户
        async commitEditForm() {
            try {
                const response = await api.admin.editAdmin(this.addForm);
                if (response.data.code === 200) {
                    this.$message.success("编辑成功");
                    this.getAdminList();
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 点击新增按钮
        add() {
            this.isAdd = true;
            this.dialogFormVisible = true;
            this.addForm = this.$options.data().addForm;
        },
        // // 删除业主
        // deleteUser(id) {
        //     this.$confirm("确认删除, 是否继续?", "提示", {
        //         confirmButtonText: "确定",
        //         cancelButtonText: "取消",
        //         type: "warning",
        //     })
        //         .then(async () => {
        //             try {
        //                 const response = await api.admin.deleteAdmin({
        //                     adminId: id,
        //                 });
        //                 if (response.data.code === 200) {
        //                     this.$message.success("删除成功");
        //                     this.getAdminList();
        //                 } else {
        //                     this.$message.error(response.data.msg);
        //                 }
        //             } catch (error) {
        //                 this.$message.error(error);
        //             }
        //         })
        //         .catch(() => {});
        // },
        // 改变禁启用状态
        async changeDeleted(id) {
            try {
                const response = await api.admin.enableAdmin({
                    id,
                });
                if (response.data.code === 200) {
                    this.$message.success("操作成功");
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        getTime(time, type = "yyyy-mm-dd") {
            return type === "yyyy-mm-dd hh:mm:ss"
                ? time[0] +
                      "-" +
                      time[1] +
                      "-" +
                      time[2] +
                      " " +
                      time[3] +
                      ":" +
                      time[4] +
                      ":" +
                      time[5]
                : time[0] + "-" + time[1] + "-" + time[2];
        },
        handleSizeChange(val) {
            this.pageInfo.size = val;
            this.getAdminList();
        },
        handleCurrentChange(val) {
            this.pageInfo.currentPage = val;
            this.getAdminList();
        },
    },
};
</script>

<style lang='less' scoped>
</style>